package com.itheima.health.controller;

import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "检查项管理开发")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询功能",notes = "查询所有的检查项列表信息")
    @PreAuthorize("hasRole('ADMIN')")
    public Result  findAll(){
        return new Result(checkItemService.list());
    }

    @PostMapping("checkitem/findPage")
    @ApiOperation(value = "查询分页功能",notes = "分页查询所有的检查项列表信息")
    public Result  findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(checkItemService.findPage(queryPageBean));
    }

    @DeleteMapping("checkitem/deleteCheckItemById/{id}")
    @ApiOperation(value = "删除功能",notes = "根据id删除检查项列表信息")
    public Result  deleteCheckItemById(@PathVariable("id")int id){
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        checkItem.setIs_delete(1);
        return new Result(checkItemService.updateById(checkItem));
    }

    @PostMapping("checkitem/saveOrUpdate")
    @ApiOperation(value = "添加或修改功能",notes = "添加新的检查项或更新原有的检查项")
    public Result saveAndUpdate(@RequestBody CheckItem checkItem){
        // 无id代表添加 如果添加操作 初始化 is_delete =0
        if (checkItem.getId()==null){
            checkItem.setIs_delete(0);
        }
        //  saveOrUpdate   添加和修改功能   判断  checkitem 有无 主键id   没有id  save    存在主键id  update
        return new Result(checkItemService.saveOrUpdate(checkItem));
    }

}
